//
//  BirthdayTableViewCell.swift
//  FearfulNotification
//
//  Created by 筱鹏 on 2018/1/16.
//  Copyright © 2018年 筱鹏. All rights reserved.
//

import UIKit

class BirthdayTableViewCell: UITableViewCell {
    
    var nameLabel: UILabel!
    var birthdayLabel: UILabel!
    var descLabel: UILabel!
    var alertSwitch: UISwitch!
    var alertClosure: ((Bool)->Void)?

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = Theme.color.background
        self.contentView.backgroundColor = Theme.color.background
        configSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("BirthdayTableViewCell init coder")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }

    func configSubviews() {

        nameLabel = UILabel()
        nameLabel.textColor = Theme.color.title
        nameLabel.font = Theme.font.title
        contentView.addSubview(nameLabel)
        
        birthdayLabel = UILabel()
        birthdayLabel.textColor = Theme.color.date
        birthdayLabel.font = Theme.font.date
        contentView.addSubview(birthdayLabel)
        
        descLabel = UILabel()
        descLabel.textColor = Theme.color.description
        descLabel.font = Theme.font.description
        contentView.addSubview(descLabel)
        
        alertSwitch = UISwitch()
        alertSwitch.tintColor = Theme.color.foreground
        alertSwitch.onTintColor = Theme.color.foreground
        alertSwitch.addTarget(self, action: #selector(BirthdayTableViewCell.alertSwitchAction(_:)), for: .valueChanged)
        contentView.addSubview(alertSwitch)
        
        configSubviewsFrame()
    }
    
    func configSubviewsFrame() {

        let leftSpace: CGFloat = 15
        let rightSpace: CGFloat = 15
        let verticalSpace: CGFloat = 5
        let horizontalSpace: CGFloat = 10
        
        nameLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        let nameTop = NSLayoutConstraint(item: nameLabel!, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1.0, constant: verticalSpace)
        let nameLeft = NSLayoutConstraint(item: nameLabel!, attribute: .left, relatedBy: .equal, toItem: contentView, attribute: .left, multiplier: 1.0, constant: leftSpace)
        let nameHeight = NSLayoutConstraint(item: nameLabel!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 37)
        NSLayoutConstraint.activate([nameTop, nameLeft, nameHeight])
        
        birthdayLabel.translatesAutoresizingMaskIntoConstraints = false
        let birthdayTop = NSLayoutConstraint(item: birthdayLabel!, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1.0, constant: verticalSpace)
        let birthdayLeft = NSLayoutConstraint(item: birthdayLabel!, attribute: .left, relatedBy: .equal, toItem: nameLabel, attribute: .right, multiplier: 1.0, constant: horizontalSpace)
        let birthdayHeight = NSLayoutConstraint(item: birthdayLabel!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 37)
        let birthdayWidth = NSLayoutConstraint(item: birthdayLabel!, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 70)
        NSLayoutConstraint.activate([birthdayTop, birthdayLeft, birthdayHeight, birthdayWidth])
        
        descLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        descLabel.translatesAutoresizingMaskIntoConstraints = false
        let descTop = NSLayoutConstraint(item: descLabel!, attribute: .top, relatedBy: .equal, toItem: nameLabel, attribute: .bottom, multiplier: 1.0, constant: verticalSpace)
        let descLeft = NSLayoutConstraint(item: descLabel!, attribute: .left, relatedBy: .equal, toItem: nameLabel, attribute: .left, multiplier: 1.0, constant: 0)
        let descHeight = NSLayoutConstraint(item: descLabel!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 18)
        NSLayoutConstraint.activate([descTop, descLeft, descHeight])
        
        alertSwitch.translatesAutoresizingMaskIntoConstraints = false
        let alertCenterY = NSLayoutConstraint(item: alertSwitch!, attribute: .centerY, relatedBy: .equal, toItem: contentView, attribute: .centerY, multiplier: 1.0, constant: 0)
        let alertRight = NSLayoutConstraint(item: alertSwitch!, attribute: .right, relatedBy: .equal, toItem: contentView, attribute: .right, multiplier: 1.0, constant: -rightSpace)
        let alertBirthdaySpace = NSLayoutConstraint(item: alertSwitch!, attribute: .left, relatedBy: .greaterThanOrEqual, toItem: birthdayLabel, attribute: .right, multiplier: 1.0, constant: horizontalSpace)
        let alertDescSpace = NSLayoutConstraint(item: alertSwitch!, attribute: .left, relatedBy: .greaterThanOrEqual, toItem: descLabel, attribute: .right, multiplier: 1.0, constant: horizontalSpace)
        NSLayoutConstraint.activate([alertCenterY, alertRight, alertBirthdaySpace, alertDescSpace])
    }
    
    @objc func alertSwitchAction(_ sw: UISwitch) {
        guard let closure = self.alertClosure else { return }
        closure(sw.isOn)
    }
}
